//
//  UIButton+Category.m
//  haitao
//
//  Created by Seven on 16/5/4.
//  Copyright © 2016年 Seven. All rights reserved.
//

#import "UIButton+Category.h"
#import <objc/runtime.h>
@implementation UIButton (EnlargeTouchArea)

static char topNameKey;
static char rightNameKey;
static char bottomNameKey;
static char leftNameKey;

- (void)setEnlargeEdgeWithTop:(CGFloat)top right:(CGFloat)right bottom:(CGFloat)bottom left:(CGFloat)left
{
    objc_setAssociatedObject(self, &topNameKey, [NSNumber numberWithFloat:top], OBJC_ASSOCIATION_COPY_NONATOMIC);
    objc_setAssociatedObject(self, &rightNameKey, [NSNumber numberWithFloat:right], OBJC_ASSOCIATION_COPY_NONATOMIC);
    objc_setAssociatedObject(self, &bottomNameKey, [NSNumber numberWithFloat:bottom], OBJC_ASSOCIATION_COPY_NONATOMIC);
    objc_setAssociatedObject(self, &leftNameKey, [NSNumber numberWithFloat:left], OBJC_ASSOCIATION_COPY_NONATOMIC);
}

- (CGRect)enlargedRect
{
    NSNumber *topEdge = objc_getAssociatedObject(self, &topNameKey);
    NSNumber *rightEdge = objc_getAssociatedObject(self, &rightNameKey);
    NSNumber *bottomEdge = objc_getAssociatedObject(self, &bottomNameKey);
    NSNumber *leftEdge = objc_getAssociatedObject(self, &leftNameKey);
    if (topEdge && rightEdge && bottomEdge && leftEdge) {
        return CGRectMake(self.bounds.origin.x - leftEdge.floatValue,
                          self.bounds.origin.y - topEdge.floatValue,
                          self.bounds.size.width + leftEdge.floatValue + rightEdge.floatValue,
                          self.bounds.size.height + topEdge.floatValue + bottomEdge.floatValue);
    }
    else
    {
        return self.bounds;
    }
}

- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event
{
    CGRect rect = [self enlargedRect];
    if (CGRectEqualToRect(rect, self.bounds)) {
        return [super hitTest:point withEvent:event];
    }
    return CGRectContainsPoint(rect, point) ? self : nil;
}

+ (instancetype)initWithFrame:(CGRect)frame{
    UIButton *btn = [super initWithFrame:frame];
    btn.frame = frame;
    return btn;
}

- (void)addTarget:(id)target action:(SEL)action{
    [self addTarget:target action:action forControlEvents:UIControlEventTouchUpInside];
}

- (void)setTitleColor:(UIColor *)color{
    [self setTitleColor:color forState:UIControlStateNormal];
    [self setTitleColor:[color colorWithAlphaComponent:.7] forState:UIControlStateHighlighted];
}

- (void)setSelectedTitleColor:(UIColor *)color{
    [self setTitleColor:color forState:UIControlStateSelected];
}

- (void)setTitleNormal:(NSString *)title{
    [self setTitle:title forState:UIControlStateNormal];
}

- (void)setSelectedTitle:(NSString *)title{
    [self setTitle:title forState:UIControlStateSelected];
}

- (void)setTitleFontWithSize:(CGFloat)fontSize{
    self.titleLabel.font = [UIFont systemFontOfSize:fontSize];
}
- (void)setTitleFont:(UIFont *)font{
    self.titleLabel.font = font;
}

- (void)setImage:(UIImage *)image{
    [self setImage:image forState:UIControlStateNormal];
}

- (void)setImageWithName:(NSString *)imageName{
    [self setImage:[UIImage imageNamed:imageName]];
}

- (void)setImageWithUrl:(NSString *)url{
    [self setImageWithURL:[NSURL URLWithString:url] forState:(UIControlStateNormal) options:nil];
}

- (void)setBackgroundImageWithUrl:(NSString *)url{
    [self setBackgroundImageWithURL:[NSURL URLWithString:url] forState:(UIControlStateNormal) options:nil];
}

- (void)setBackgroundImage:(UIImage *)image{
    [self setBackgroundImage:image forState:UIControlStateNormal];
}

- (void)setBackgroundImageWithName:(NSString *)imageName{
    [self setBackgroundImage:[UIImage imageNamed:imageName]];
}

- (void)setSelectedImage:(UIImage *)image{
    [self setImage:image forState:UIControlStateSelected];
}

- (void)setSelectedImageWithName:(NSString *)imageName{
    [self setImage:[UIImage imageNamed:imageName] forState:UIControlStateSelected];
}

- (void)setHighlightedImageWithName:(NSString *)imageName{
    [self setImage:[UIImage imageNamed:imageName] forState:UIControlStateHighlighted];
}

- (UIImageView *)theImageView{
    for (UIImageView *view in self.subviews) {
        if ([view isKindOfClass:[UIImageView class]]) {
            return view;
        }
    }
    return nil;
}
- (UILabel *)theTitleLabel{
    for (UILabel *view in self.subviews) {
        if ([view isKindOfClass:[UILabel class]]) {
            return view;
        }
    }
    return nil;
}
- (CGFloat)titleLabelWidth{
    return self.titleLabel.intrinsicContentSize.width;
}

#pragma mark - (void)addPressAnimation

- (void)addPressAnimation{
    [self addTarget:self action:@selector(touchDown:) forControlEvents:UIControlEventTouchDown];
    //[self addTarget:self action:@selector(touchUp:) forControlEvents:UIControlEventTouchUpInside];
    //[self addTarget:self action:@selector(touchUp:) forControlEvents:UIControlEventTouchCancel];
}

- (void)touchDown:(UIButton *)sender{
    
    CABasicAnimation *scaleAnimation = [CABasicAnimation animationWithKeyPath:@"transform.scale"];
    scaleAnimation.duration = .1;
    scaleAnimation.autoreverses = YES;
    scaleAnimation.repeatCount = 0.0;
    scaleAnimation.fromValue = [NSNumber numberWithFloat:1];
    scaleAnimation.toValue = [NSNumber numberWithFloat:.96];
    [self.layer addAnimation:scaleAnimation forKey:@"scaleAnimation"];
}

@end
